package com.kgc.dao;

import com.kgc.entity.Car;
import com.kgc.entity.Room;
import com.kgc.entity.UserCar;
import com.kgc.entity.UserRoom;
import com.kgc.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CarDao extends BaseDao{
    public List<Car> selectPage(PageUtils pageUtils, String name) {
        List<Car> list = new ArrayList<>();
        try {
            String sql = "select * from car ";
            if (StringUtils.isNotEmpty(name)) { // 字符串是否为空
                sql += " where name like '%\" + name + \"%'";
            }
            sql += " limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Car(rs.getInt(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getInt(4)
                )
                );
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }

    public int totalCount(String status) {
        try {
            String sql = "select count(*) from car ";
            if (StringUtils.isNotEmpty(status)) { // 字符串是否为空
                sql += " where status like '%" + status + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    public int addCar(Car car) {
        String sql = "INSERT INTO `car`(`name`,`type`,`status`) VALUES (?,?,?)";
        return super.getExecuteUpdate(sql, car.getName(),car.getType(),car.getStatus());
    }

    public int deleteCar(String id) {
        String sql = "delete from `car` where id = ?";
        return super.getExecuteUpdate(sql, id);
    }

    public int updateCar(Car car) {
        String sql = "UPDATE `car` SET `name` = ?, `type`=?,`status`=? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, car.getName(), car.getType(), car.getStatus(),car.getId());
    }

    public List<UserCar> selectUserPage(PageUtils pageUtils, String carId) {
        List<UserCar> list = new ArrayList<>();
        try {
            String sql = "select uc.*,u.username from user_car uc left join user u on uc.user_id = u.id where uc.car_id=? ";
            sql += " limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, carId,pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new UserCar(rs.getInt(1), rs.getInt(2), rs.getInt(3),rs.getDate(4)+" "+rs.getTime(4),rs.getDate(5)+" "+rs.getTime(5),rs.getString(6)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }

    public int userTotalCount(String carId) {
        try {
            String sql = "select count(*) from user_car where car_id=?";
            ResultSet rs = super.getExecuteQuery(sql,carId);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
}
